package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/contractProduct")
public class ContractProductController extends BaseController {
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private FactoryService factoryService;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/list", name = "查询货物列表")
    public String findAll(String contractId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        ContractProductExample example = new ContractProductExample();
        example.createCriteria().andContractIdEqualTo(contractId);

        PageInfo pageInfo = contractProductService.findAll(example, page, pageSize);
        request.setAttribute("page", pageInfo);

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> list = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", list);

        request.setAttribute("contractId", contractId);
        return "cargo/product/product-list";
    }

    @RequestMapping(value = "/edit", name = "保存货物信息")
    public String save(ContractProduct contractProduct, MultipartFile productPhoto) {
        String upload = null;
        try {
            if (productPhoto != null) {
                upload = fileUploadUtil.upload(productPhoto);
            }
        } catch (Exception e) {
        }
        contractProduct.setProductImage(upload);

        if (StringUtils.isEmpty(contractProduct.getId())) {
            contractProduct.setId(UUID.randomUUID().toString());
            contractProduct.setCompanyId(getCompanyId());
            contractProduct.setCompanyName(getCompanyName());
            contractProductService.save(contractProduct);
        } else {
            contractProductService.update(contractProduct);
        }
        String contractId = contractProduct.getContractId();
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }

    @RequestMapping(value = "/toUpdate", name = "跳转到修改货物页面")
    public String toUpdate(String id) {
        ContractProduct contractProduct = contractProductService.findById(id);
        request.setAttribute("contractProduct", contractProduct);

        FactoryExample example = new FactoryExample();
        example.createCriteria().andCtypeEqualTo("货物");
        List<Factory> list = factoryService.findAll(example);
        request.setAttribute("factoryList", list);
        return "cargo/product/product-update";
    }

    // 货物删除之后要一同删除其下的附件
    @RequestMapping(value = "/delete", name = "删除货物信息")
    public String delete(String id, String contractId) {
        contractProductService.delete(id);
        request.setAttribute("contractId", contractId);
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }

    @RequestMapping(value = "/toImport", name = "上传货物")
    public String toImport(String contractId) {
        request.setAttribute("contractId", contractId);
        return "cargo/product/product-import";
    }

    @RequestMapping(value = "/import", name = "上传货物")
    public String productImport(String contractId, MultipartFile file) {
        try {
            InputStream fileInputStream = file.getInputStream();
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0);
            int rowNum = sheet.getLastRowNum(); // 获取最后一行的索引值
            List<ContractProduct> list = new ArrayList<>();
            for (int i = 1; i <= rowNum; i++) {
                ContractProduct product = new ContractProduct();
                Row row = sheet.getRow(i);
                String factoryName = row.getCell(1).getStringCellValue();
                product.setFactoryName(factoryName);
                String productNo = row.getCell(2).getStringCellValue();
                product.setProductNo(productNo);
                int cNumber = (int) row.getCell(3).getNumericCellValue();
                product.setCnumber(cNumber);
                String packingUnit = row.getCell(4).getStringCellValue();
                product.setPackingUnit(packingUnit);
                String loadingRate = row.getCell(5).getNumericCellValue() + "";
                product.setLoadingRate(loadingRate);
                int boxNum = (int) row.getCell(6).getNumericCellValue();
                product.setBoxNum(boxNum);
                double price = row.getCell(7).getNumericCellValue();
                product.setPrice(price);
                String productDesc = row.getCell(8).getStringCellValue();
                product.setProductDesc(productDesc);
                String productRequest = row.getCell(9).getStringCellValue();
                product.setProductRequest(productRequest);

                product.setId(UUID.randomUUID().toString());
                product.setContractId(contractId);
                product.setCreateDept(getUser().getDeptName());
                product.setCreateBy(getUser().getId());
                product.setCreateTime(new Date());
                product.setCompanyId(getUser().getCompanyId());
                product.setCompanyName(getUser().getCompanyName());

                list.add(product);
            }
            contractProductService.saveAll(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }
}
